<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb class="navigation">
      <el-breadcrumb-item :to="{ path: '/mapm' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>首页管理</el-breadcrumb-item>
      <el-breadcrumb-item>公告管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体区域 -->
    <el-card class="notice">
      <h4>&nbsp;&nbsp;公告管理</h4>
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="请输入内容" v-model="queryInfo.name" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getNotice"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addNoticeVisible = true">新增</el-button>
        </el-col>
      </el-row>
      <!--文件数据区-->
      <el-table :data="noticeList" border stripe height="488px">
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column label="标题" prop="title"> </el-table-column>
        <el-table-column label="发布者" prop="createByuser"> </el-table-column>
        <el-table-column label="发布时间" prop="createTime"> </el-table-column>
        <el-table-column label="操作" width="270px">
          <template slot-scope="scope">
            <el-tooltip  effect="dark" content="编辑" placement="top" :enterable="false">
            <el-button type="primary"  icon="el-icon-edit" size="mini" @click="toNoticeDetail(scope.row.id)"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteNotice(scope.row.id)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--分页区域-->
    <!-- 新增公告 -->
    <!-- 新增公告 -->
    <el-dialog :visible.sync="addNoticeVisible" width="40%">
      <span>
      <el-form :model="addNoticeForm" ref="addNoticeForm">
        <div class="title">新增公告</div>
      <div class="hint">公告标题</div>
      <el-input v-model="addNoticeForm.title" placeholder="请输入文章标题"></el-input>
      <div class="hint">公告内容</div>
        <editor v-model="addNoticeForm.content"></editor>
      </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button :plain="true" @click="open('addNoticeForm')">取 消</el-button>
        <el-button type="primary" @click="toAddNoticeForm('addNoticeForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 查看公告 -->
    <el-dialog :visible.sync="checkNoticeVisible" width="40%" @close="editDialogClosed">
      <span>
        <div class="title">政策详情</div>
        <el-form :model="editForm" ref="editFormRef" label-width="70px">
          <el-form-item label="文章标题" prop="title">
            <el-input v-model="editForm.title"></el-input>
          </el-form-item>
          <el-form-item label="文章内容" prop="content">
            <editor v-model="editForm.content"></editor>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="checkNoticeVisible = false">取 消</el-button>
          <el-button type="primary" @click="editNoticeInfo">确 定</el-button>
        </span>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import Editor from '../../../common/editor'
export default {
  name: 'announcement',
  components: {
    axios,
    Editor
  },
  data () {
    return {
      // 区域列表
      optionList: [],
      input: '',
      userId: '',
      queryInfo: {
        // 当前页码
        pageNum: 1,
        pageSize: 5,
        name: ''
      },
      total: 0,
      // 政策文章列表
      noticeList: [],
      user: [],
      name: '',
      editorOption: {},
      // 新增政策文章
      addNoticeForm: {},
      editForm: {},
      // 控制对话框的显示与隐藏
      addNoticeVisible: false,
      checkNoticeVisible: false
    }
  },
  created () {
    this.getNotice()
  },
  methods: {
    // 获取公告列表
    getNotice () {
      this.$axios.get('pogofficialinfo/getarticlebypage', {
        params: this.queryInfo
      })
        .then(res => {
          if (res.data.code === 401) {
            console.log(res.data)
            this.$message({
              message: '获取文件数据失败',
              type: 'error',
              offset: 70
            })
          }
          this.noticeList = res.data.data.list
          this.total = res.data.total
          console.log(res)
        }).catch(err => {
          console.log(err)
        })
    },
    // 查看文章内容
    async toNoticeDetail (id) {
      const { data: res } = await this.$axios.get('pogofficialinfo/' + id)
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('查询失败！')
      }
      this.editForm = res.data
      this.checkNoticeVisible = true
    },
    // 删除文章
    async deleteNotice (noticeId) {
      const confirmResult = await this.$confirm(
        '此操作将删除文章, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      console.log(noticeId)
      const { data: res } = await this.$axios.delete('pogofficialinfo/' + noticeId)
      if (res.code !== 200) {
        return this.$message.error(res.message)
      }
      this.$message.success('删除成功！')
      this.getNotice()
    },
    // 控制对话框关闭消息发送
    open (addNoticeForm) {
      this.$message('已取消增加')
      this.$refs[addNoticeForm].resetFields()
      this.addNoticeVisible = false
    },
    // 监听修改对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 提交表单
    toAddNotionForm (addNotionForm) {
      this.$refs[addNotionForm].validate((valid) => {
        if (valid) {
          let param = {
            title: this.addNotionForm.title,
            context: this.addNotionForm.context
          }
          this.$axios({
            url: 'pogofficialinfo/addinfo',
            method: 'post',
            data: param
          }).then(res => {
            console.log(res.data)
            if (res.data.code === 200) {
              this.$message.success(res.data.message)
              this.addNotionVisible = false
              this.getNotion()
              this.$refs[addNotionForm].resetFields()
            }
          })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    // 修改文章内容并提交
    editNoticeInfo () {
      this.$refs.editFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await this.$axios.post('pogofficialinfo/addinfo', {
            title: this.editForm.title,
            content: this.editForm.content
          })
          if (res.code !== 200) {
            this.$message.error(res.message)
          }
          this.$message.success('修改成功！')
          this.checkNoticeVisible = false
          this.getNotice()
        }
      })
    },

    // 监听pagesize改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pageSize = newSize
      this.getNotice()
    },
    // 监听页码值改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pageNum = newPage
      this.getNotice()
    }
  }
}
</script>

<style scoped>

.navigation {
  padding: 10px;
}
.notice {
  margin-bottom: 40px;
  border-radius: 15px;
  box-shadow: 15px 15px 10px -12px #4c61a0;
}
h4 {
  padding-bottom: 8px;
  margin-bottom: 20px;
  border-bottom: 2px solid #4c61a0;
}
.title {
  text-align: center;
  font-size: 25px;
  border-bottom: 2px solid #4c61a0;
  padding-bottom: 20px;
  margin-bottom: 30px;
}
.hint {
  margin: 10px 0;
  font-size: 18px;
}
.selectRange {
  text-align-last: center;
}

</style>
